package com.newdjk.newdoctor.constants;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.HashSet;
import java.util.Set;

public class EnvironmentManager {
    private static final String PREFERENCE_NAME = "_environment";
    public static final int ENVIRONMENT_TEST = 1;
    public static final int ENVIRONMENT_ONLINE = 2;
    public static final int ENVIRONMENT_EMPTY_VALUE = -1;

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    }



    public static void switchEnvironment(Context context,int envType) {
        SharedPreferences pre = getSharedPreferences(context);
        SharedPreferences.Editor editor = pre.edit();
        editor.putInt("env", envType);
        editor.commit();
        System.exit(0);
    }

    /**
     * get current environment type, if no value is found ,{@link #ENVIRONMENT_EMPTY_VALUE} will be returned;
     *
     * @param context
     * @return {@link #ENVIRONMENT_EMPTY_VALUE}, {@link #ENVIRONMENT_TEST}, {@link #ENVIRONMENT_PRE_PUBLISH}, {@link #ENVIRONMENT_PUBLISH}
     */
    public static int getEnvironmentType(Context context) {
        SharedPreferences pre = getSharedPreferences(context);
        return pre.getInt("env", ENVIRONMENT_EMPTY_VALUE);
    }

    /**
     * @param type only one of {@link #ENVIRONMENT_TEST}, {@link #ENVIRONMENT_PRE_PUBLISH}, {@link #ENVIRONMENT_PUBLISH}
     */
    public static void saveEnvironmentType(Context context, int type) {
        SharedPreferences pre = getSharedPreferences(context);
        SharedPreferences.Editor editor = pre.edit();
        editor.putInt("env", type);
        editor.commit();
    }

    public static boolean isDebug(Context context) {
        SharedPreferences pre = getSharedPreferences(context);
        int istest = pre.getInt("env", ENVIRONMENT_EMPTY_VALUE);
        if (istest==ENVIRONMENT_ONLINE){
            AppConfig.TEST=false;
            return false;
        }else {
            AppConfig.TEST=true;
            return true;
        }
    }

    private static Set<Activity> sActivityStack = new HashSet<>();

    public static void addActivity(Activity activity) {
        sActivityStack.add(activity);
    }

    public static void removeActivity(Activity activity) {
        sActivityStack.add(activity);
    }

    public static void finishActivities() {
        for (Activity act : sActivityStack) {
            if (act != null)
                act.finish();
        }
        sActivityStack.clear();
    }

    public static void clearActivities() {
        sActivityStack.clear();
    }
}
